
<?php
	 $html->initHelper('CustomFieldDisplay');
		$customFieldDisplay = $html->CustomFieldDisplay;
$disableEditorReviews = cr('GeneralConfig.disable_editor_reviews');
$categoryIds = set::extract('{n}.Listing.list_category_id',$listings);
$categoryIds = array_unique($categoryIds);

foreach($listings as $listing)
{
	if(!isset($wrapperListings[$listing['Listing']['list_category_id']]))
	{
		$wrapperListings[$listing['Listing']['list_category_id']] = array();
	}
	
	$wrapperListings[$listing['Listing']['list_category_id']][] = $listing;
}
if($listings)
{
	foreach($wrapperListings as $categoryId=>$listings)
	{	
		$listingCount = count($listings)-1;
		?>
		<h2>
			<?php
			if(isset($categoryInfo[$categoryId]))
			{
				echo $categoryInfo[$categoryId].'('. count($listings) .')';
			}?>
		</h2>
		<?php 
		if($listingCount < 1)
		{
			echo __('Only 1 listing selected in this category, select more than one listing to compare.');
			continue;
		}?>	
		<!--code to show fields info starts here-->
		<table class="compare-table">
			<tr class="first">
				<?php
                //@todo Implement ListingInformationHelper on Fieldset also.
				foreach($listings as $index=>$listingData)
				{
					$counterListingnumber = $index;
					//code to prepare custom fields with fieldset starts here
					$FieldPropetiesObject = $ListingModelObj->getAttributes($listingData);
					$showFields = $FieldPropetiesObject->getFields();
                    $fieldproperies = $FieldPropetiesObject->getFieldProperties();
					$i = 0;
					$newFieldproperies = array();
					foreach($fieldproperies as $field=>$fieldInfo)
					{
						if(isset($fieldInfo['fieldset_name']))
						{
							$newFieldproperies[$fieldInfo['fieldset_name']][$i][$field] = $fieldInfo;
							$newFieldproperies[$fieldInfo['fieldset_name']]['fieldsetInfo']['fieldset_label'] = $fieldInfo['fieldset_label'];
							$newFieldproperies[$fieldInfo['fieldset_name']]['fieldsetInfo']['fieldset_show_label'] = $fieldInfo['fieldset_show_label'];
							$i++;
						}	
					}
					$customFields = array();
				
					$fieldGroupInfoArray = array();
					foreach($newFieldproperies as $fieldGroup=>$fieldGroutpInfo)
					{	
						$fieldGroupInfoArray[$fieldGroup] = $fieldGroutpInfo['fieldsetInfo'];
						unset($fieldGroutpInfo['fieldsetInfo']);
						foreach($fieldGroutpInfo as $index=>$fields)
						{			
							foreach($fields as $fieldName=>$fieldInfo)
							{				
								$customFields[$fieldName] = $fieldInfo['label'];
								$fieldGroupArr[$fieldName] = $fieldGroup;
							}
						}
					}
					//code to prepare custom fields with fieldset ends here
					?>
					<?php if($counterListingnumber == 0)
					{?>
						<th>
						</th>
						<?php 
					}?>
					<td class ='main_listing'>
						<span class="fr">
							<?php
							if(count($listings) > 2)
							{ 
								//$this->params['named']['listing_ids'] = "".$this->params['named']['listing_ids']."";
								echo $html->link($html->image('/img/btn_remove.gif',array()),'javascript:void(0);',array('onclick'=>'return removeListing(\''.$this->params['named']['listing_ids'].'\','.$listingData['Listing']['id'].');','escape'=>false));
							}?>
						</span><br/>
						<center>
							<?php
							//showing main listing image
							if(!empty($listingData['Listing']['defaultImageViewUrl']))
							{
								echo $html->link($html->image($listingData['Listing']['image'],array('thumb'=>array('crop'=>true,'width'=>147,'height'=>88),'class'=>'thumbnail')),$listingData['Listing']['defaultImageViewUrl'],array('escape'=>false));
							}else
							{
								echo $html->image($listingData['Listing']['image'],array('thumb'=>array('crop'=>true,'width'=>147,'height'=>88),'class'=>'thumbnail'));
							}						
							?>
						</center>
						<br/><b><?php echo $html->link($listingData['Listing']['title'],$listingData['Listing']['viewUrl']);?></b>
						
						
					</td>				
				<?php 
				}?>
			</tr>			
			<?php
			$fields = array(
				'description'=>__('Description'),
				'rating'=>__('Rating'),
				'edirotRating'=>__('Editor Rating')
			);
			if($disableEditorReviews == 1)
			{
				unset($fields['edirotRating']);
			}
			$fields = am($fields, $customFields);
			$executedFieldGroup = false;
			foreach($fields as $field=>$label)
			{	
				?>
				<?php if(isset($fieldGroupArr[$field]) && $executedFieldGroup != $fieldGroupArr[$field])
				{?>
					<tr>
						<th colspan="<?php echo count($listings)+1;?>" >
												<?php 
						$fieldFieldGroupName = $fieldGroupArr[$field];
						if($fieldGroupInfoArray[$fieldFieldGroupName]['fieldset_show_label'])
						{?>
									<b><?php echo ucwords($fieldGroupInfoArray[$fieldFieldGroupName]['fieldset_label']);?></b>
						<?php }
						?>
							<?php $executedFieldGroup = $fieldGroupArr[$field];?>
						</th>				
					</tr>
				<?php }?>
					<tr>
						<th><?php echo $label;?></th>
						<?php
						for($i = 0;$i<=$listingCount;$i++)
						{
                            $displayValue = "";
                            $FieldPropetiesObject = $ListingModelObj->getAttributes($listings[$i]);
                            switch($field)
                            {
                              case 'rating':
                                  $displayValue = $rating->display(array('rating'=>$listings[$i]['Listing']['userAvg'],'tooltip'=>__('User Rating').' '.number_format($listings[$i]['Listing']['userAvg'],1)));
                                break;
                              case 'edirotRating':
                                  $displayValue = $rating->display(array('rating'=>$listings[$i]['Listing']['editorialAvg'],'class'=>'editor_rating_star_value','tooltip'=>__('Editor Rating').' '.number_format($listings[$i]['Listing']['editorialAvg'],1)));
                                break;
                              case 'description':
                                    $displayValue = $listings[$i]['Listing']['description'];
                                    if(cr('ListingConfig.flay_text'))
                                    {
                                       $displayValue = $appHtml->flayText($displayValue,cr('ListingConfig.flay_text'));
                                    }
                                break;
                                default:
                                  $displayValue = $listingInformation->showCompareListingField($listings[$i],$field);
      
                            }
							?>
							
							<td>
							<?php
                                echo $displayValue;
                            ?>
							</td>
							<?php
					 	}?>	
					</tr>				
			<?php
			}?>		
		</table>
	<br />	
<?php }//for look end
}//main if block?>
	
<script type="text/javascript">
function removeListing(oldListingIds,listingId)
{
	url = URL+'listings/compare/'+'listing_ids:'+oldListingIds+'/removal_listing:'+listingId;
	window.location = url;
}
</script>
